<?php

class UserInfo_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $row
     * @return UserInfo
     */
    public static function getUserInfoByRow($row)
    {
        $thatUserInfo = new UserInfo;
        $thatUserInfo->setId($row->id);
        $thatUserInfo->setRealName($row->real_name);
        $thatUserInfo->setSex($row->sex);
        $thatUserInfo->setProv($row->prov);
        $thatUserInfo->setCity($row->city);
        $thatUserInfo->setDist($row->dist);
        $thatUserInfo->setPhone($row->phone);
        $thatUserInfo->setQq($row->qq);
        $thatUserInfo->setProfile($row->profile);
        $thatUserInfo->setAvatarHash($row->avatar_hash);
        $thatUserInfo->setUserId($row->user_id);
        $thatUserInfo->setPhoneShort($row->phone_short);
        $thatUserInfo->setBirthday($row->birthday);
        $thatUserInfo->setDefaultContactType($row->DefaultContactType);
        return $thatUserInfo;
    }

    /**
     * @param UserInfo $thatUserInfo
     * @return array
     */
    public static function getModelByUserInfo($thatUserInfo)
    {
        return array(
            'real_name'   => $thatUserInfo->getRealName(),
            'sex'         => $thatUserInfo->getSex(),
            'prov'        => $thatUserInfo->getProv(),
            'city'        => $thatUserInfo->getCity(),
            'dist'        => $thatUserInfo->getDist(),
            'phone'       => $thatUserInfo->getPhone(),
            'qq'          => $thatUserInfo->getQq(),
            'profile'     => $thatUserInfo->getProfile(),
            'avatar_hash' => $thatUserInfo->getAvatarHash(),
            'user_id'     => $thatUserInfo->getUserId(),
            'phone_short' => $thatUserInfo->getPhoneShort(),
            'birthday'    => $thatUserInfo->getBirthday(),
            'DefaultContactType'=>$thatUserInfo->getDefaultContactType(),
        );
    }

    /**
     * @param int $id
     * @return UserInfo
     */
    public static function getUserInfoById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('user_info.*');
        $CI->db->from('user_info');
        $CI->db->where('user_info.id', $id);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getUserInfoByRow($row);
        else
            return null;
    }

    /**
     * @param int $userId
     * @return UserInfo
     */
    public static function getUserInfoByUserId($userId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('user_info.*');
        $CI->db->from('user_info');
        $CI->db->where('user_info.user_id', $userId);

        $query = $CI->db->get();
        $row   = $query->row();
        if ($row)
            return self::getUserInfoByRow($row);
        else
            return null;
    }

    /**
     * @param UserInfo $thatUserInfo
     * @throws DatabaseException
     * @return User
     */
    public static function add($thatUserInfo)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $result = $CI->db->insert('user_info', self::getModelByUserInfo($thatUserInfo));
        if ($result)
            return self::getUserInfoById($CI->db->insert_id());
        else
            throw new DatabaseException('插入数据失败');
    }

    /**
     * @param UserInfo $thatUserInfo
     * @throws DatabaseException
     * @return UserInfo
     */
    public static function update($thatUserInfo)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatUserInfo->getID());
        $result = $CI->db->update('user_info', self::getModelByUserInfo($thatUserInfo));
        if ($result)
            return self::getUserInfoById($thatUserInfo->getID());
        else
            throw new DatabaseException('更新数据失败');
    }

    /**
     * @param UserInfo $thatUserInfo
     * @throws DatabaseException
     * @return bool
     */
    public static function delete($thatUserInfo)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatUserInfo->getID());
        $result = $CI->db->delete('user_info');
        if ($result)
            return true;
        else
            throw new DatabaseException('删除数据失败');
    }

    /**
     * @param UserInfo $thatUserInfo
     * @throws DatabaseException
     * @return bool
     */
    public static function save($thatUserInfo)
    {
        if ($thatUserInfo->getId() >= 0) {
            return self::update($thatUserInfo);
        } else {
            return self::add($thatUserInfo);
        }
    }

}
